package com.li.day.day17;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class zhuce extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       resp.setCharacterEncoding("gbk");

        Connection co=null;
        PreparedStatement pp=null;
        String s = "INSERT INTO ppro(user_name,user_password) VALUES(?,?)";
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
             co = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/p3kao", "root", "root");
             pp = co.prepareStatement(s);
            String name = req.getParameter("name1");
            String mima = req.getParameter("mima1");
            String mima2 = req.getParameter("mima2");
            if (mima.equals(mima2)){
                pp.setString(1,name);
                pp.setString(2,mima);
                pp.executeUpdate();
                   req.getRequestDispatcher("/WEB-INF/denglu.jsp").forward(req,resp);
            }else{
                resp.getWriter().write("请检查密码");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (pp!=null){
                try {
                    pp.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (co!=null){
                try {
                    co.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
